﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace Issue
{
    class Work
    {
        public int s = 0;
        public void GetNum()
        {
            string str = string.Empty;
            for (int i = 0; i < 10000; i++)
            {
                WebClient webClient = new WebClient();
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                StringBuilder result = new StringBuilder();
                result.Append("fild1");
                result.Append("=");
                result.Append(s);
                result.Append("&");
                result.Append("fild2");
                result.Append("=");
                result.Append(i.ToString());

                Uri u = new Uri("http://192.168.2.5:83/Default.aspx");
                webClient.UploadString(u, "post", result.ToString());
                str += result.ToString() + "\r\n";
            }
            WriteFile(s, str);
            MessageBox.Show(s + "   完成");
        }

        /// <summary>
        /// 向文件写入内容并根据bool append参数决定是改写还是追加 append=false则改写
        /// </summary>
        public void WriteFile(int s, string fileContent)
        {
            string file = @"D:\test\SqliteTest\" + s + ".txt";
            FileInfo info = new FileInfo(file);
            if (!Directory.Exists(info.DirectoryName))
            {
                Directory.CreateDirectory(info.DirectoryName);
            }
            lock (info)
            {
                StreamWriter writer = new StreamWriter(file, true, Encoding.GetEncoding("utf-8"));
                try
                {
                    try
                    {
                        writer.Write(fileContent);
                    }
                    catch (Exception exception)
                    {
                        throw new FileNotFoundException(exception.ToString());
                    }
                }
                finally
                {
                    writer.Flush();
                    writer.Close();
                }
            }
        }
    }
}
